{{- define "admission_controller_resources" }}
cpu: 25m
memory: 100Mi
{{- end }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: vpa-admission-controller
  namespace: kube-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "vpa-admission-controller" "workload-resource-policy.deckhouse.io" "master")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: vpa-admission-controller
  updatePolicy:
    updateMode: "Initial"
  resourcePolicy:
    containerPolicies:
    - containerName: admission-controller
      minAllowed:
        {{- include "admission_controller_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 50m
        memory: 200Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: vpa-admission-controller
  namespace: kube-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "vpa-admission-controller")) | nindent 2 }}
spec:
  {{- include "helm_lib_deployment_on_master_strategy_and_replicas_for_ha" . | nindent 2 }}
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: vpa-admission-controller
  template:
    metadata:
      annotations:
        checksum/extension-apiserver-authentication: {{ .Values.global.discovery.extensionAPIServerAuthenticationRequestheaderClientCA | sha256sum }}
        checksum/certificate: {{ include (print .Template.BasePath "/admission-controller/tls-certs.yaml") . | sha256sum }}
      labels:
        app: vpa-admission-controller
    spec:
      {{- include "helm_lib_priority_class" (tuple . "system-cluster-critical") | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "vpa-admission-controller")) | nindent 6 }}
      {{- include "helm_lib_node_selector" (tuple . "master") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node" "with-uninitialized") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" . | nindent 6 }}
      serviceAccountName: d8-vertical-pod-autoscaler-admission-controller
      imagePullSecrets:
      - name: deckhouse-registry
      containers:
      - name: admission-controller
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "admissionController") }}
        command:
        - /admission-controller
        - --address=:8944
        - --tls-cert-file=/etc/tls-certs/serverCert.pem
        - --tls-private-key=/etc/tls-certs/serverKey.pem
        - --stderrthreshold=0
        - --logtostderr=true
        - --alsologtostderr=true
        - --register-webhook=false
        - --v=4
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
          - name: tls-certs
            mountPath: "/etc/tls-certs"
            readOnly: true
        ports:
        - containerPort: 8000
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
{{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "admission_controller_resources" . | nindent 12 }}
{{- end }}
      volumes:
        - name: tls-certs
          secret:
            secretName: vpa-tls-certs
---
apiVersion: v1
kind: Service
metadata:
  name: vpa-webhook
  namespace: kube-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "vpa-admission-controller")) | nindent 2 }}
spec:
  ports:
    - port: 443
      targetPort: 8000
  selector:
    app: vpa-admission-controller
